﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using ObjectManager;

namespace QuanLyDiem
{
    public partial class frmMain : Form
    {
        private DangNhap login = null;

        public frmMain()
        {
            InitializeComponent();
            this.IsMdiContainer = true;
        }

        private void mniAbout_Click(object sender, EventArgs e)
        {
            frmAbout frm = frmAbout.Create();
            frm.ShowDialog();
        }

        private void HideForm()
        {
            foreach (Form item in MdiChildren)
            {
                if (item.OwnedForms.Length > 0)
                {
                    for (int i = 0; i < item.OwnedForms.Length; i++)
                    {
                        item.OwnedForms[i].Close();
                    }
                }
                item.Hide();
            }
        }

        private void HideForm(Form form)
        {
            foreach (Form item in MdiChildren)
            {
                if (item != form)
                {
                    if (item.OwnedForms.Length > 0)
                    {
                        for (int i = 0; i < item.OwnedForms.Length; i++)
                        {
                            item.OwnedForms[i].Close();
                        }
                    }
                    item.Hide();
                }
            }
        }

        private void CloseForm()
        {
            foreach (Form item in MdiChildren)
            {
                item.Close();
            }
        }

        private void mniLogin_Click(object sender, EventArgs e)
        {
            EnableMenu(menuStrip1, false);
            mniLogin.Enabled = true;
            frmLogin frm = frmLogin.Create();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.LoginSuccess += new Login(frm_LoginSuccess);
            frm.Show();
        }

        private void frm_LoginSuccess(object sender, ObjectManager.InfoLogin e)
        {
            login = e.Login;
            slblHello.Text = "Xin chào: " + login.ThongTin.Ten + " (" + login.ThongTin.TenQuyen + ")";
            EnableMenu(menuStrip1, true);
            mniLogin.Enabled = false;
            mniDoiMK.Visible = true;

            if (e.Login.ThongTin.Quyen == 1)
            {
                mniDoiMK.Visible = false;
            }
            if (e.Login.ThongTin.Quyen > 1)
            {
                mniNguoiDung.Enabled = false;
            }

            frmDiemSV frm = frmDiemSV.Create(true);
            frm.MdiParent = this;
            frm.Show();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            EnableMenu(menuStrip1, false);
            mniLogin.Enabled = true;
            frmLogin frm = frmLogin.Create();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.LoginSuccess += new Login(frm_LoginSuccess);
            frm.Show();
        }
        
        private void EnableMenu(MenuStrip menu, bool enable)
        {
            foreach (ToolStripMenuItem item in menu.Items)
            {
                EnableMenuItem(item, enable);
            }
            mniExit.Enabled = true;
            mniAbout.Enabled = true;
        }

        private void EnableMenuItem(ToolStripMenuItem tsmi, bool enable)
        {
            if (tsmi.DropDownItems.Count > 0)
            {
                foreach (ToolStripMenuItem item in tsmi.DropDownItems)
                {
                    if (enable)
                    {
                        item.Enabled = enable;
                        EnableMenuItem(item, enable);
                    }
                    else
                    {
                        item.Enabled = enable;
                        EnableMenuItem(item, enable);
                    }
                }
            }
        }

        private void mniLogout_Click(object sender, EventArgs e)
        {
            //CloseForm();
            HideForm();
            EnableMenu(menuStrip1, false);
            mniLogin.Enabled = true;
            slblHello.Text = "";
            frmLogin frm = frmLogin.Create();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.LoginSuccess += new Login(frm_LoginSuccess);
            frm.Show();
        }

        private void mniExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void mniSinhVien_Them_Click(object sender, EventArgs e)
        {
            frmSinhVien_ThayDoi frm = frmSinhVien_ThayDoi.Create(true);
            frm.MdiParent = this;
            frm.Show();
        }

        private void mniSinhVien_ThayDoi_Click(object sender, EventArgs e)
        {
            HideForm();
            frmSinhVien_Them frm = frmSinhVien_Them.Create(true);
            frm.MdiParent = this;
            frm.Show();
        }

        private void mniDoiMK_Click(object sender, EventArgs e)
        {
            if (login.ThongTin.Quyen < 1)
            {
                frmDoiMatKhau_0 frm = frmDoiMatKhau_0.Create(login.ThongTin.TenDangNhap);
                frm.ShowDialog();
            }
            else
            {
                frmDoiMatKhau frm = frmDoiMatKhau.Create(login.ThongTin.TenDangNhap);
                frm.ShowDialog();
            }
        }

        private void mônHọcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HideForm();
            frmDiemMH frm = frmDiemMH.Create(true);
            frm.MdiParent = this;
            frm.Show();
        }

        private void sinhViênToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            HideForm();
            frmDiemSV frm = frmDiemSV.Create(true);
            frm.MdiParent = this;
            frm.Show();
        }

        private void thôngTinToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HideForm();
            frmBoMon_ThayDoi frm = frmBoMon_ThayDoi.Create(true);
            frm.MdiParent = this;
            frm.Show();
        }

        private void thêmToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HideForm();
            frmBoMon_Them frm = frmBoMon_Them.Create(true);
            frm.MdiParent = this;
            frm.Show();
        }

        private void thôngTinToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            HideForm();
            frmLop_ThayDoi frm = frmLop_ThayDoi.Create(true);
            frm.MdiParent = this;
            frm.Show();
        }

        private void thêmToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            HideForm();
            frmLop_Them frm = frmLop_Them.Create(true);
            frm.MdiParent = this;
            frm.Show();
        }

        private void thôngTinToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            HideForm();
            frmMonHoc_ThayDoi frm = frmMonHoc_ThayDoi.Create(true);
            frm.MdiParent = this;
            frm.Show();
        }

        private void thêmToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            HideForm();
            frmMonHoc_Them frm = frmMonHoc_Them.Create(true);
            frm.MdiParent = this;
            frm.Show();
        }

        private void họcBổngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HideForm();
            frmHocBong frm = frmHocBong.Create(true);
            frm.MdiParent = this;
            frm.Show();
        }

        private void mniNguoiDung_Click(object sender, EventArgs e)
        {
            HideForm();
            frmThongTinDangNhap frm = frmThongTinDangNhap.Create(true);
            frm.MdiParent = this;
            frm.Show();
        }

        private void inDanhSáchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HideForm();
            frmDSDiemMH frm = frmDSDiemMH.Create(true);
            frm.MdiParent = this;
            frm.Show();
        }

        private void đăngKýToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HideForm();
            frmDangKy frm = frmDangKy.Create(true);
            frm.MdiParent = this;
            frm.Show();
        }
    }
}
